package com.zyg.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.User;
import com.zyg.pojo.UserRole;
import com.zyg.service.UserRoleService;
import com.zyg.service.UserService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;


    @SaCheckLogin
    @RequestMapping("/tolist")
    public  String tolist(){
        return "user/list";
    }

    @SaCheckPermission("user:list")
    @RequestMapping("/list")
    @ResponseBody
    public  String list(User user, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        // 查询数据
        LayData layData = new LayData();
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (user.getName() != null && !"".equals(user.getName()))
        {
            qw.like("name", user.getName());
        }
        if (user.getPass() != null && !"".equals(user.getPass()))
        {
            qw.like("pass", user.getPass());
        }
        long totalCount = userService.count(qw);
        layData.setCount((int) totalCount);

        Page<User> page1 = new Page<>(page, limit);

        List<User> list = userService.list(page1, qw);
        layData.setData(list);
        layData.setCode(0);
        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);
        return data;
    }
    @SaCheckPermission("user:add")
    @RequestMapping("/toadd")
    public String toAdd(){
        return "user/add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(User user){
        boolean result = userService.add(user);
        if (result) {
            return JSON.toJSONString(new LayData(0, "添加成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "添加失败"));
        }
    }
    @SaCheckPermission("user:update")
    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model){
        User user = userService.findUserById(id);
        QueryWrapper<UserRole> qw = new QueryWrapper<>();
        qw.eq("u_id", id);
        List<UserRole> userRoles = userRoleService.list(qw);
        //jdk8新特性
        List<Integer> roleIds = userRoles.stream().map(UserRole::getRId).toList();

        model.addAttribute("user", user);
        model.addAttribute("roleIds", roleIds);
        return "user/update";
    }

    @RequestMapping("/toinfo/{id}")
    public String toinfo(@PathVariable("id") int id, Model model){
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "user/info";
    }
    @SaCheckPermission("user:update")
    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(User user){
        boolean result = userService.update(user);
        if (result) {
            return JSON.toJSONString(new LayData(0, "更新成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "更新失败"));
        }
    }
    @SaCheckPermission("user:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteUser(@RequestParam("ids") List<Integer> ids){
        boolean result = userService.removeBatchByIds(ids);
        if (result) {
            return JSON.toJSONString(new LayData(0, "删除成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "删除失败"));
        }
    }


}